package ua.com.ulitko.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellBrowser;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import ua.com.ulitko.client.entity.Performer;
import ua.com.ulitko.client.service.PerformerService;
import ua.com.ulitko.client.service.PerformerServiceAsync;

import java.util.ArrayList;

public class Main implements EntryPoint {
    PerformerServiceAsync serv = PerformerService.Inst.getInstance();

    public void onModuleLoad() {
        ServiceDefTarget endpoint = (ServiceDefTarget) serv;
        endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "PerformerService");

        serv.getPerformerList(new AsyncCallback<ArrayList<Performer>>() {
            @Override
            public void onFailure(Throwable caught) {
                System.out.println(caught.getMessage());
            }

            @Override
            public void onSuccess(ArrayList<Performer> result) {
                CellBrowser browser = new CellBrowser(new CustomTreeModel(), result);
                browser.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

                LayoutPanel layoutPanel = new LayoutPanel();
                layoutPanel.add(browser);
                layoutPanel.setWidgetLeftWidth(browser, 0, Style.Unit.PCT, 40, Style.Unit.PCT);  // Left panel
                RootLayoutPanel.get().add(layoutPanel);

                Button button = new Button("add");
                button.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        ClientUtil.setToRightPanel(new AddSongPanel());
                    }
                });
                RootPanel.get("addLayer").add(button);
            }
        });
    }
}
